﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Business;
using NHibernate;
using ZLCMS.Data.Model;
using NHibernate.Criterion;
using ZLCMS.Business.DwzFrame;
using ZLCMS.Business.Util;
using ZLCMS.Business.TranferParam;
using Newtonsoft.Json;
using System.Text;
using DotNet.Common.TransferParam;
using System.Xml;
using log4net;
using System.Reflection;

namespace ZLCMS.Web.SystemManage.Basics
{
    public partial class OrderPayed : System.Web.UI.Page
    {
        protected FrameHelper _framePage;

        protected ShopOrderInfo _orderDb;

        protected void Page_Load(object sender, EventArgs e)
        {
            //初始化DWZ控件
            _framePage = new FrameHelper(this);

            //初始化全局变量值
            InitializeGlobalValue();

            //初始化控件值
            InitializeContent();

            if (IsPostBack)
            {
                Save();
            }
        }

        protected void InitializeContent()
        {
            //页面控件数据

            //页面控件值
            if (!IsPostBack)
            {

            }
        }

        protected void InitializeGlobalValue()
        {
            string oid;

            try
            {
                if (!IsPostBack)
                {
                    oid = Request["uid"] == null ? "" : Request["uid"];
                    _oid.Text = oid;
                }
                else
                {
                    oid = _oid.Text;
                }

                using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
                {
                    _orderDb = session.CreateCriteria<ShopOrderInfo>().Add(Expression.Eq("OrderNo", oid)).UniqueResult<ShopOrderInfo>();
                }

                if (_orderDb == null)
                {
                    throw new Exception("找不到对应参数信息.");
                }
            }
            catch (System.Exception ex)
            {
                _framePage.Alert(new ReturnCode() { statusCode = 300, message = "操作出错-" + ex.Message });
                return;
            }
        }

        protected void Save()
        {
            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                try
                {
                    _orderDb.PayStatus = "1";
                    _orderDb.ManageHistory += string.Format("<br><font color=red>【{0}】确认支付，({1})</font><br>{2}", UserKit.GetUserAccount(), DateTime.Now, tb_memo.Text);

                    session.Update(_orderDb);
                    session.Flush();
                }
                catch (System.Exception ex)
                {
                    _framePage.Alert(new ReturnCode() { statusCode = 300, message = "操作出错-" + ex.Message });
                    return;
                }
            }

            // 刷新 editparamgroup 页面
            _framePage.Alert(new ReturnCode()
            {
                statusCode = 200,
                message = "操作成功!",
                callbackType = "closeCurrent",
                navTabId = "vieworder"
            });
        }
    }
}
